﻿using Alphacat.Model;
using AlphaCat.View;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Alphacat
{
    public partial class Form1 : Form
    {

        //BackgroundWorker qrcodeWorker;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //qrcodeWorker = new BackgroundWorker();
            //qrcodeWorker.DoWork += new DoWorkEventHandler(loadQRCodeAndShow);
            //qrcodeWorker.RunWorkerAsync();

            string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/AlphaCat";
            if (Directory.Exists(docPath) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(docPath);
            }

            DoLogin();
        }

        /*
        private void loadQRCodeAndShow(object sender, DoWorkEventArgs e)
        {
            LoginService loginService = new LoginService();
            Image qrcodeImage = loginService.GetQRCode();
            this.pictureBox1.Image = qrcodeImage;
        }
        */

        private void DoLogin()
        {
            //picQRCode.Image = null;
            //picQRCode.SizeMode = PictureBoxSizeMode.Zoom;
            //lblTip.Text = "手机微信扫一扫以登录";
            ((Action)(delegate ()
            {
                //异步加载二维码
                LoginService ls = new LoginService();
                Image qrcode = ls.GetQRCode();
                if (qrcode != null)
                {
                    this.BeginInvoke((Action)delegate ()
                    {
                        QRpictureBox.Image = qrcode;
                    });

                    object login_result = null;
                    while (true)  //循环判断手机扫面二维码结果
                    {
                        login_result = ls.LoginCheck();
                        if (login_result is Image) //已扫描 未登录
                        {
                            this.BeginInvoke((Action)delegate ()
                            {
                                msgLabel.Text = "请点击手机上登录按钮";
                                //QRpictureBox.SizeMode = PictureBoxSizeMode.CenterImage;  //显示头像
                                QRpictureBox.Image = login_result as Image;
                                //linkReturn.Visible = true;
                            });
                        }
                        if (login_result is string)  //已完成登录
                        {
                            //访问登录跳转URL
                            Console.WriteLine("login redirect: " + login_result);
                            ls.GetSidUid(login_result as string);

                            //打开主界面
                            this.BeginInvoke((Action)delegate ()
                            {
                                //this.Hide();
                                
                                TaskForm mf = new TaskForm();
                                mf.Show();
                                this.Hide();
                                //frmMainForm frmmf = new frmMainForm();
                                //frmmf.Show();
                            });
                            break;
                        }
                    }
                }
            })).BeginInvoke(null, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ((Action)delegate ()
            {
                QRCode.SaveBitmapPngFile("2017061112345678");
            }).BeginInvoke(null, null);
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/AlphaCat/test.docx";
            string res = BaseService.UploadServer(docPath);
            Console.WriteLine("////////上传test.doc结果////////");
            Console.WriteLine(res);
        }
    }
}
